Newer
Older
taehui / taehui-fe / src / app / [language] / commentary / query / usePutCommentary.ts
@Taehui Taehui on 17 Mar 877 bytes 2024-03-17 오후 2:12
import { isClientFault, wwwAPI } from "@/utilities/wwwAPI";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useTranslations } from "next-intl";
import { toast } from "react-toastify";

export default function usePutCommentary() {
  const queryClient = useQueryClient();

  const t = useTranslations();

  return useMutation({
    mutationFn: async ({
      commentaryID,
      avatarCipher,
      text,
    }: {
      commentaryID: number;
      avatarCipher: string;
      text: string;
    }) => {
      await wwwAPI.put("/commentary", {
        commentaryID,
        avatarCipher,
        text,
      });
    },
    onSuccess: async () => {
      await queryClient.invalidateQueries({ queryKey: ["commentary"] });
    },
    onError: (e) => {
      if (isClientFault(e)) {
        toast.error(t("failedValidateCipher"));
      }
    },
  });
}